using System;

namespace CSharp._Util
{
	class Helper
	{
		public static string GetNullableAliasVariableType(Type type)
		{
			if( IsValueType(type) )
			{
				return GetAliasVariableType(type) + "?";
			}
			
			return GetAliasVariableType( type );
		}
		
		#region GetOlnyAliasVariableType
		public static string GetOlnyAliasVariableType(Type type)
		{
			return GetOlnyAliasVariableType(type.FullName);
		}
		
		public static string GetOlnyAliasVariableType(string type)
		{
			switch (type)
			{
				case "System.String": return "string";
				case "System.Byte": return "byte";
				case "System.Byte[]": return "byte[]";			
				case "System.Int16": return "short";
				case "System.Int32": return "int";
				case "System.Int64": return "long";
				case "System.Char": return "char";
				case "System.Single": return "float";
				case "System.Double": return "double";
				case "System.Boolean": return "bool";
				case "System.Decimal": return "decimal";
				case "System.SByte": return "sbyte";
				case "System.UInt16": return "ushort";
				case "System.UInt32": return "uint";
				case "System.UInt64": return "ulong";
				case "System.Object": return "object";
				default:
					return null;
			}
		}
		#endregion
		
		#region GetAliasVariableType
		public static string GetAliasVariableType(Type type)
		{
			return GetAliasVariableType(type.FullName);
		}
		
		public static string GetAliasVariableType(string type)
		{
			string alias = GetOlnyAliasVariableType(type);
			if( alias == null )
			{
				string nameSpace = GetNamespace(type);				
				if (nameSpace == "System")
					type = GetClassName(type);
						
				return type;
			}
			
			return alias;
		}
		#endregion
		
		#region IsValueType
		public static bool IsValueType( object o )
		{
			return o.GetType().IsSubclassOf(Type.GetType("System.ValueType"));
		}
		
		public static bool IsValueType( Type type )
		{
			return type.IsSubclassOf(Type.GetType("System.ValueType"));
		}
		#endregion
		
		public static string GetClassName(string name)
		{
			string[] namespaces = name.Split(new Char[] {'.'});
			return namespaces[namespaces.Length-1];
		}
		
		public static string GetNamespace(string name)
		{
			string[] namespaces = name.Split(new Char[] {'.'});
			return String.Join(".", namespaces, 0, namespaces.Length-1);		
		}
		
		public static string GetPrivateVariableName( string name )
		{
			string pvName = null;
			if( name != null )
			{
				if( name != "" )
				{
					pvName = "_";
					pvName += name[0].ToString().ToLower();
					pvName += name.Substring(1);
				}
				else
					pvName = "";
			}
			
			return pvName;
		}
		
		public static string GetPropertiesName( string name )
		{
			string pName = null;
			if( name != null )
			{
				if( name != "" )
				{
					pName += name[0].ToString().ToUpper();
					pName += name.Substring(1);
				}
				else
					pName = "";
			}
			
			return pName;
		}
		
		public static string GetSubVariableName( string name )
		{
			string svName = null;
			if( name != null )
			{
				if( name != "" )
				{
					svName += name[0].ToString().ToLower();
					svName += name.Substring(1);
				}
				else
					svName = "";
			}
			
			return svName;
		}
		
		public static string GetCSharpString( string str )
		{
			return GetCSharpString( str, "" );
		}
		
		public static string GetCSharpString( string str, string Backspace )
		{
			return str.Replace( "\"", "\\\"" ).Replace( "\r\n", "\\r\\n\" +\r\n" + Backspace + "	\"" );
		}
	}
}